//
//  OAuthViewController.m
//  dwdWbOc
//
//  Created by Dengdwd on 15/10/9.
//  Copyright © 2015年 dwd. All rights reserved.
//

#import "OAuthViewController.h"
#import "SVProgressHUD.h"
#import "NetworkTools.h"
@interface OAuthViewController ()<UIWebViewDelegate>
@property (strong, nonatomic) IBOutlet UIWebView *webView;

@end

@implementation OAuthViewController
static NSString * client_id = @"3529494176";
static NSString * redirect_uri = @"http://www.baidu.com";

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadWebViewRequest];
}
- (void)loadWebViewRequest {
    NSString * urlString = @"https://api.weibo.com/oauth2/authorize";
    
    urlString = [NSString stringWithFormat:@"%@?client_id=%@&redirect_uri=%@",urlString,client_id,redirect_uri];
    
    NSURL * url = [[NSURL alloc]initWithString:urlString];
    
    NSURLRequest * request = [NSURLRequest requestWithURL:url];
    
    [self.webView loadRequest:request];
}

- (IBAction)close:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)autoLogin:(id)sender {
   NSString * jsString = @"document.getElementById('userId').value = '530249530@qq.com';document.getElementById('passwd').value = 'Twandi0225'";
    [self.webView stringByEvaluatingJavaScriptFromString:jsString];
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSString * urlString = request.URL.absoluteString;
    if ([urlString hasPrefix:@"https://api.weibo.com"]) {
        return YES;
    }
    if (![urlString hasPrefix:redirect_uri]) {
        return NO;
    }

    NSString * query = request.URL.query;
    if (query) {
        NSString * code = @"code=";
        code = [query substringFromIndex:code.length];
        
    }
    
    
    return NO;
    
}
-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [SVProgressHUD dismiss];
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    [SVProgressHUD show];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSString * title = @"document.title";
    self.title = [self.webView stringByEvaluatingJavaScriptFromString:title];
    [SVProgressHUD dismiss];
}


@end
